package com.dld.coupon.util;

import com.dld.android.util.LogUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Random;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.message.BasicHeader;

public class SimpleMultipartEntity implements HttpEntity {
    private static final char[] MULTIPART_CHARS = "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
            .toCharArray();
    private String boundary = null;
    boolean isSetFirst = false;
    boolean isSetLast = false;
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    public SimpleMultipartEntity() {
        StringBuffer localStringBuffer = new StringBuffer();
        Random localRandom = new Random();
        for (int i = 0; i < 30; i++)
            localStringBuffer.append(MULTIPART_CHARS[localRandom
                    .nextInt(MULTIPART_CHARS.length)]);
        this.boundary = localStringBuffer.toString();
    }

    public void addPart(String paramString1, File paramFile, String paramString2) {
        try {
            addPart(paramString1, paramFile.getName(), new FileInputStream(
                    paramFile), paramString2);
            return;
        } catch (FileNotFoundException localFileNotFoundException) {
            while (true)
                LogUtils.e("test", localFileNotFoundException.getMessage(),
                        localFileNotFoundException);
        }
    }

    public void addPart(String paramString1, String paramString2) {
        addPart(paramString1, paramString2, false);
    }

    // ERROR //
    public void addPart(String paramString1, String paramString2,
            InputStream paramInputStream, String paramString3) {
       
    }

    public void addPart(String paramString1, String paramString2,
            boolean paramBoolean) {
        writeFirstBoundaryIfNeeds();
        try {
            this.out.write(("Content-Disposition: form-data; name=\""
                    + paramString1 + "\"\r\n").getBytes());
            this.out.write("Content-Type: text/plain; charset=UTF-8\r\n"
                    .getBytes());
            this.out.write("Content-Transfer-Encoding: 8bit\r\n\r\n".getBytes());
            this.out.write(paramString2.getBytes());
            ByteArrayOutputStream localByteArrayOutputStream = this.out;
            StringBuilder localStringBuilder = new StringBuilder("\r\n--")
                    .append(this.boundary);
            if (paramBoolean)
                ;
            for (String str = "--";; str = "\r\n") {
                localByteArrayOutputStream.write(str.getBytes());
                break;
            }
        } catch (IOException localIOException) {
            LogUtils.e("test", localIOException.getMessage(), localIOException);
        }
    }

    public void addPart(String paramString1, String paramString2,
            byte[] paramArrayOfByte, String paramString3) {
        writeFirstBoundaryIfNeeds();
        try {
            String str = "Content-Type: " + paramString3 + "\r\n";
            this.out.write(("Content-Disposition: form-data; name=\""
                    + paramString1 + "\"; filename=\"" + paramString2 + "\"\r\n")
                    .getBytes());
            this.out.write(str.getBytes());
            this.out.write("Content-Transfer-Encoding: binary\r\n\r\n"
                    .getBytes());
            this.out.write(paramArrayOfByte, 0, paramArrayOfByte.length);
            this.out.flush();
            return;
        } catch (IOException localIOException) {
            while (true)
                LogUtils.e("test", localIOException.getMessage(),
                        localIOException);
        }
    }

    public void consumeContent() throws IOException,
            UnsupportedOperationException {
        if (!isStreaming())
            return;
        throw new UnsupportedOperationException(
                "Streaming entity does not implement #consumeContent()");
    }

    public InputStream getContent() throws IOException,
            UnsupportedOperationException {
        return new ByteArrayInputStream(this.out.toByteArray());
    }

    public Header getContentEncoding() {
        return null;
    }

    public long getContentLength() {
        writeLastBoundaryIfNeeds();
        return this.out.toByteArray().length;
    }

    public Header getContentType() {
        return new BasicHeader("Content-Type", "multipart/form-data; boundary="
                + this.boundary);
    }

    public boolean isChunked() {
        return false;
    }

    public boolean isRepeatable() {
        return false;
    }

    public boolean isStreaming() {
        return false;
    }

    public void writeFirstBoundaryIfNeeds() {
        if (!this.isSetFirst)
            ;
        try {
            this.out.write(("--" + this.boundary + "\r\n").getBytes());
            this.isSetFirst = true;
            return;
        } catch (IOException localIOException) {
            while (true)
                LogUtils.e("test", localIOException);
        }
    }

    public void writeLastBoundaryIfNeeds() {
        if (this.isSetLast)
            ;
        // while (true)
        {
            // return;
            try {
                this.out.write(("\r\n--" + this.boundary + "--\r\n").getBytes());
                this.isSetLast = true;
            } catch (IOException localIOException) {
                // while (true)
                // LogUtils.e("test", localIOException.getMessage(),
                // localIOException);
            }
        }
    }

    public void writeTo(OutputStream paramOutputStream) throws IOException {
        paramOutputStream.write(this.out.toByteArray());
    }
}
